package org.chen.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.chen.common.exception.ExceptionCodeEnum;
import org.chen.common.exception.FlyingException;
import org.chen.common.token.FrontTokenComponent;
import org.chen.common.user.User;
import org.chen.mapper.UserMapper;
import org.chen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private FrontTokenComponent tokenComponent;

    /**
     * 获取当前用户id
     *
     * @return Integer
     */
    @Override
    public Integer getUserIdException() {
        Integer id = tokenComponent.getUserId();
        if (null == id) {
            throw new FlyingException(ExceptionCodeEnum.UNAUTHORIZED,"登录信息已过期，请重新登录！");
        }
        return id;
    }

    @Override
    public User getTravelInfoException() {
        User user = getTravelInfo();
        if (user == null) {
            throw new FlyingException(ExceptionCodeEnum.UNAUTHORIZED,"用户信息不存在！");
        }

        if (!user.getStatus()) {
            throw new FlyingException("用户已经被禁用！");
        }
        return user;
    }

    @Override
    public User getTravelInfo() {
        if (getTravelUserId() == 0) {
            return null;
        }
        //假如返回的用户id是1，对应的用户为张三...
        User user = new User();
        user.setAccount("zhangsan");
        user.setUid(1);
        return user;
//        return getById(getUserId());
    }

    private Integer getTravelUserId() {
        Integer id = tokenComponent.getTravelUserId();
        if (null == id) {
            return 0;
        }
        return id;
    }
}
